<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入目录名称" clearable @change="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-arrow-left" size="mini" @click="goOut()">返回</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['detail:detail:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="detailList" :tree-props="{ children: 'children' }" row-key="id">
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="创建机构" align="center" prop="deptName" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="文件模板" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="openUploadAdd(scope.row)"
            :disabled="scope.row.createJunior == '0'">上传</el-button>
          <el-button size="mini" type="text" @click="openUploadAdd(scope.row)"
            :disabled="scope.row.createJunior == '0'">预览</el-button>
          <el-button size="mini" type="text" @click="openUploadAdd(scope.row)"
            :disabled="scope.row.createJunior == '0'">下载</el-button>
          <el-button size="mini" type="text" @click="openUploadAdd(scope.row)"
            :disabled="scope.row.createJunior == '0'">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column label="是否需要创建下级" align="center" prop="createJunior">
        <template slot-scope="scope">
          <el-switch :value="scope.row.createJunior" active-value="0" inactive-value="1"
            @change="(value) => handleStatusChange(value, scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.createJunior == '0'" size="mini" type="text"
            @click="handleAdddate(scope.row)">新建下级</el-button>
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
            v-hasPermi="['detail:detail:edit']">修改</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)"
            v-hasPermi="['detail:detail:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改业内资料目录-详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="目录id" prop="catalogId" v-if="!$route.query.id">
          <el-input v-model="form.catalogId" placeholder="请输入目录id" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input type="number" v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <!-- <el-form-item label="创建机构" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入创建机构" />
        </el-form-item> -->
        <el-form-item label="是否需要创建下级" prop="createJunior">
          <el-radio-group v-model="form.createJunior">
            <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.value">{{ dict.label
            }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="父级id" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入父级id" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 新建下级 -->
    <el-dialog :title="title" :visible.sync="openData" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="父级" prop="id">
          <el-input v-model="form.id" v-show="false" />
          <el-input v-model="form.parentName" disabled />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input type="number" v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormAdd">确 定</el-button>
        <el-button @click="openData = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="上传文件" :visible.sync="openUpload" width="700px" append-to-body>
      <FileUpload ref="fileUpload" :value="fileList"></FileUpload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitUpload">确 定</el-button>
        <el-button @click="openUpload = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  listDetail,
  getDetail,
  delDetail,
  addDetail,
  updateDetail,
  submitFormAdd,
  relateDetailAttaches
} from "@/api/biz/catalogDetail/catalogDetail";
export default {
  name: "Detail",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      // 遮罩层
      loading: true,
      fileList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 业内资料目录-详情表格数据
      detailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openData: false,
      openUpload: false,
      // 查询参数
      queryParams: {
        // pageNum: 1,
        // pageSize: 10,
        catalogId: this.$route.query.id,
        name: null,
        deptId: null,
        createJunior: null,
        parentId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询业内资料目录-详情列表 */
    getList() {
      this.loading = true;
      listDetail(this.queryParams).then((response) => {
        if (response.code == 200) {
          this.detailList = this.handleTree(response.data, "id", "parentId");
        }
        // this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        catalogId: this.$route.query.id,
        name: null,
        deptId: null,
        createJunior: null,
        parentId: null,
        parentName: void 0,
        sort: 1,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.createJunior = "1";
      this.title = "添加业内资料目录-详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getDetail(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改业内资料目录-详情";
      });
    },
    // 新增子级
    handleAdddate(row) {
      this.reset();
      const id = row.id || this.ids;
      getDetail(id).then((response) => {
        this.form.id = response.data.id;
        this.form.parentName = response.data.name;
        (this.form.name = null), (this.openData = true);
        this.title = "新增子级";
      });
    },
    // 新增子级提交按钮
    submitFormAdd() {
      let data = {
        id: this.form.id,
        name: this.form.name,
        createJunior: 1,
      };
      submitFormAdd(data).then((response) => {
        this.$modal.msgSuccess("新增子级成功");
        this.openData = false;
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateDetail(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetail(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除业内资料目录-详情编号为"' + ids + '"的数据项？')
        .then(function () {
          return delDetail(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "detail/detail/export",
        {
          ...this.queryParams,
        },
        `detail_${new Date().getTime()}.xlsx`
      );
    },
    //修改创建下级状态
    handleStatusChange(value, row) {
      let params = {
        id: row.id,
        createJunior: value
      }
      updateDetail(params).then((response) => {
        this.$modal.msgSuccess("修改成功");
        row.createJunior = value;
        this.open = false;
        this.getList();
      })
    },
    goOut() {
      this.$router.push({ name: "Catalog" });
    },
    submitUpload() {
      const fileUpload = this.$refs.fileUpload;
      const fileIds = fileUpload.getFileList().map(item => item.id)
      let params = {
        catalogDetailId: this.form.id,
        attachIds: fileIds
      }
      relateDetailAttaches(params).then((response) => {
        this.$modal.msgSuccess("提交成功");
        this.openUpload = false;
        this.getList();
      });
    },
    openUploadAdd(row) {
      const id = row.id || this.ids;
      getDetail(id).then((response) => {
        this.form = response.data;
        this.openUpload = true;
        this.fileList = response.data.bizSafeDocAttachList.map((item) => {
          return Object.assign(item, {
            name: item.displayName + "." + item.extName,
            url: process.env.VUE_APP_MINIO_BASE_API + "/" + item.bucket + "/" + item.realName
          })
        });
      });
    },
  },
  watch: {},
};
</script>
